package m.jclecai.com.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import m.jclecai.com.R;
import m.jclecai.com.bean.TrendDataBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.ScreenUtil;
import m.jclecai.com.utils.StringUtils;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.view.DDTrendChart;
import m.jclecai.com.view.LottoTrendView;

/**
 * Created by ZD on 2017/9/11.
 */

public class BetTrendFragment extends Fragment {
    @BindView(R.id.ltv_trendView)
    LottoTrendView ltvTrendView;
    @BindView(R.id.rg_num)
    RadioGroup rgNum;
    @BindView(R.id.lotteryBody)
    View lotteryBody;
    //当前的玩法
    String modes;
    //走势图数据
    String data;
    //走势图
    private DDTrendChart mTrendChart;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_lotto_trend, container,false);
        ButterKnife.bind(this,inflate);
        initView();
        return inflate;
    }

    public void initView() {
        this.mTrendChart = new DDTrendChart(getActivity(), this.ltvTrendView);
        this.ltvTrendView.setChart(this.mTrendChart);
        this.mTrendChart.setShowYilou(true);
        this.mTrendChart.setDrawLine(true);
    }

    public void getMissList(String type, String mode) {
        this.modes = mode;
        OkHttpClientManager.Param Url = new OkHttpClientManager.Param("url", Constant_Url.GET_MISS_LIST);
        OkHttpClientManager.Param Type = new OkHttpClientManager.Param("type", type);
        OkHttpClientManager.Param[] param = new OkHttpClientManager.Param[]{Url, Type};
        try {
            OkHttpClientManager.getInstance().asynPost(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    data = json;
                    addRadioButton();
                }

                @Override
                public void onError() {
                    if(getActivity()!=null){
                        ToastUtil.showToast(getActivity(), R.string.connect_failure);
                    }
                }
            }, param);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void setMode(String mode) {
        this.modes = mode;
        addRadioButton();
    }

    /**
     * 动态添加按钮
     *
     */
    void addRadioButton() {//第一位 第二位
        rgNum.removeAllViews();
        int count = 0;
        switch (modes) {
            case "前一直选":
                modes = "前零直选";
                count = 0;
                break;
            case "前二直选":
                count = 2;
                break;
            case "前三直选":
                count = 3;
                break;
            case "前四直选":
                count = 4;
                break;
            case "前五直选":
                count = 5;
                break;
        }
        int width = ScreenUtil.getScreenWidth(getActivity());
        //这里表示是否有多个按钮，比如选择位置的，
        //这里主要有如下的几个玩法，支持按位选择：前2，前3，
        for (int i = 1; i <= count; i++) {
            RadioButton rb = new RadioButton(getActivity());
            rgNum.addView(rb, new LinearLayout.LayoutParams(width / count, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f));
            rb.setCompoundDrawablesWithIntrinsicBounds(null, null, null, ContextCompat.getDrawable(getActivity(), R.drawable.tab_radio_driver));
            rb.setButtonDrawable(R.color.transparent);
            rb.setTextColor(ContextCompat.getColor(getActivity(), R.color.color_333));
            rb.setGravity(Gravity.CENTER);
            rb.setText("第"+translateNum(i)+"位");
            rb.setTag("直选"+translateNum(i));
            rb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    modes = v.getTag().toString();
                    updateTrendChart();
                }
            });
            if (i == 1) {
                rgNum.check(rb.getId());
                modes = rb.getTag().toString();
            }
        }
        if (count <= 1) {
            //没有按位的直接隐藏；
            rgNum.setVisibility(View.GONE);
        }else {
            //有按照位置的显示；
            rgNum.setVisibility(View.VISIBLE);
        }
        updateTrendChart();
    }

    String translateNum(int num) {
        String name = "";
        switch (num) {
            case 1:
                name = "一";
                break;
            case 2:
                name = "二";
                break;
            case 3:
                name = "三";
                break;
            case 4:
                name = "四";
                break;
            case 5:
                name = "五";
                break;
        }
        return name;
    }

    public void updateTrendChart() {
        //这里是处理第一次显示走势图时 view高度充满限定区域一屏 33是与默认宽高
        ArrayList<TrendDataBean> trendDataBeen = StringUtils.string2JsonArray(data, modes);
        float v = lotteryBody.getHeight() -
                (rgNum.getVisibility() == View.GONE ? 0 : ScreenUtil.dip2px2(getActivity(), 40));
        mTrendChart.fillScreen(lotteryBody.getWidth(),v,trendDataBeen.size() + 1,true);

        mTrendChart.updateData(trendDataBeen, modes.contains("直选") , 11, true,
                false, 0, ContextCompat.getColor(getActivity(), R.color.trend_ball_red), true, false,
                "","前零直选".equals(modes));
    }
}
